package com.example.buyticketlishi.wightview;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.baselibrary.Utils.SelfToastUtil;
import com.example.baselibrary.Utils.ToastUtil;
import com.example.buyticketlishi.R;
import com.example.buyticketlishi.adapter.SelectTimeAdapter;
import com.example.buyticketlishi.adapter.TimeSelectItemAdapter;
import com.example.buyticketlishi.bean.RzfindTimeControlListResultBean;

import java.util.ArrayList;
import java.util.List;

public class TimeSelectDialogShow extends BottomDialogBase {

    private Context context;
    //    private GridView gvTimeSelect;
    private Button btConfig;
    private Button btCancle;
    private RecyclerView rlItem;
    private int peopleNum;
    public boolean isAllSelect = false;
    public TimeSelectAllListener timeSelectAllListener;
    private List<String> selectdata;

    public TimeSelectDialogShow(final int pos, Context context, final List<RzfindTimeControlListResultBean.DataBean> timeSelectList, int number) {
        super(context);
        this.context = context;
        peopleNum = number;

        TimeSelectItemAdapter timeSelectItemAdapter = new TimeSelectItemAdapter(context, timeSelectList, number);
        // 给recycleView 设置布局
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        rlItem.setLayoutManager(linearLayoutManager);
        rlItem.setAdapter(timeSelectItemAdapter);

        //点击取消按钮
        btCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        timeSelectItemAdapter.SetIsAllSelectListener(new TimeSelectItemAdapter.IsAllSelectListener() {
            @Override
            public void isAllsetect(List<String> Selectdata) {
                if (selectdata == null) {
                    selectdata = new ArrayList<>();
                }
                selectdata.clear();
                for (int i = 0; i < Selectdata.size(); i++) {
                    selectdata.add(Selectdata.get(i));
                }
                isAllSelect = true;
            }
        });

        btConfig.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //如果点击确定，就要校验所有条目是否有被点击
                if (isAllSelect) {
                    if (selectdata == null || selectdata.size() <= 0) {
                        SelfToastUtil.getInstance().show("请先选择时段");
                        return;
                    }
//                    SelfToastUtil.getInstance().show("条目已经全部选择");
                    // 条目 全部选择，可以关闭，然后去显示了
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < selectdata.size(); i++) {
//                        timeSelectList.get(i).getTimeControlList().get(Integer.parseInt(selectdata.get(i))).setClickPos();
                        sb.append(timeSelectList.get(i).getTimeControlList().get(Integer.parseInt(selectdata.get(i))).getControlStartTime().substring(0, 5) + "-" + timeSelectList.get(i).getTimeControlList().get(Integer.parseInt(selectdata.get(i))).getControlEndTime().substring(0, 5) + "  ");
                    }

                    // pos 是列表的那一项
                    timeSelectAllListener.tiemSelectResult(pos,sb.toString(),selectdata);
                    dismiss();
                } else {
                    SelfToastUtil.getInstance().show("条目未全部选择");
                }

//                for (int i = 0; i < timeSelectList.size(); i++) {
////                    timeSelectList.get(i).getProjectId()
//                }
            }
        });
    }

    @Override
    protected void onCreate() {
        setContentView(R.layout.wight_time_select);

//        gvTimeSelect = findViewById(R.id.gv_time_select);
        btConfig = findViewById(R.id.bt_time_select_confirm);
        btCancle = findViewById(R.id.bt_time_select_cancle);
        rlItem = findViewById(R.id.rv_time_select_item);
    }

    public interface TimeSelectAllListener {
        void tiemSelectResult(int pos,String timeSelectString,List<String> dataList);
    }

    public void SetTimeSelectAllListener(TimeSelectAllListener selectAllListener) {
        timeSelectAllListener = selectAllListener;
    }
}
